package com.wpw.dream.frame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ArrayAndList {

	public static void main(String[] args) {
		testTime();
//		removeError();
//		ConcurrentModificationExceptionTest();
//		rongliang();
	}

	private static void rongliang() {
		long start = System.currentTimeMillis();
		ArrayList<Integer> list = new ArrayList<>();
		for (int i = 0; i < 100000; i++) {
			list.add(i);
		}
		System.out.println(System.currentTimeMillis() - start);
		start = System.currentTimeMillis();

		ArrayList<Integer> list2 = new ArrayList<>(100000);
		for (int i = 0; i < 100000; i++) {
			list2.add(i);
		}
		System.out.println(System.currentTimeMillis() - start);
	}

	public static void ConcurrentModificationExceptionTest() {
		ArrayList<Integer> list = new ArrayList<>();
		for (int i = 0; i <= 6; i++) {
			list.add(i);
		}
		Iterator<Integer> it = list.iterator();
		Iterator<Integer> it2 = list.iterator();
		while (it.hasNext()) {
			it.next();
			it.remove();
			it2.next();
			}
		
	}
	
	public static void removeError() {
		//Exception in thread "main" java.util.ConcurrentModificationException
		//ConcurrentModificationException 异常有所了解。当方法检测到对象的并发修改，但不允许这种修改时就抛出该异常。同时需要注意的是，该异常不会始终指出对象已经由不同线程并发修改，如果单线程违反了规则，同样也有可能会抛出改异常
		ArrayList<Integer> list = new ArrayList<>();
		list.add(1);
		list.add(2);
		list.add(3);
		for (int i = 0; i < list.size(); i++) {
			if (i == 1) {
				list.remove(i);
				Integer integer = list.get(2);
				System.out.println(integer);
			}
		}
	}
	
	public static void testTime() {
		int[] arrays = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		Integer sum = 0;

		Long time1 = System.currentTimeMillis();
		for (int i = 0; i < 100000000; i++) {
			sum += arrays[i % 10];
		}
		Long time2 = System.currentTimeMillis();
		System.out.println("数组求和所花费时间：" + (time2 - time1) + "毫秒");

		
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < arrays.length; i++) {
			list.add(arrays[i]);
		}
		Integer sum2 = 0;
		Long time3 = System.currentTimeMillis();
		for (int i = 0; i < 100000000; i++) {
			sum2 += list.get(i % 10);
		}
		Long time4 = System.currentTimeMillis();
		System.out.println("List求和所花费时间：" + (time4 - time3) + "毫秒");
	}
	
	public static void linkedListTest() {
		LinkedList<String> linkedList = new LinkedList<>();
		linkedList.add("dd");
		linkedList.add("11");
		List<String> synchronizedList = Collections.synchronizedList(linkedList);
//		Collections.synchronized**
	}
}
